package com.bxait.toymail.product.service.impl;

import com.bxait.com.exception.BizCodeEnum;
import com.bxait.com.exception.product.CheckOutException;
import com.bxait.com.utils.PageUtils;
import com.bxait.com.utils.Query;
import com.bxait.toymail.product.entity.AttrEntity;
import com.bxait.toymail.product.entity.CategoryEntity;
import com.bxait.toymail.product.service.AttrService;
import com.bxait.toymail.product.service.CategoryService;
import com.bxait.toymail.product.utils.AttrGroupWithAttrsVo;
import com.bxait.toymail.product.utils.CateGoryCheck;
import com.bxait.toymail.product.vo.SpuItemAttrGroupVo;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.stream.Collectors;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import com.bxait.toymail.product.dao.AttrGroupDao;
import com.bxait.toymail.product.entity.AttrGroupEntity;
import com.bxait.toymail.product.service.AttrGroupService;

import javax.annotation.Resource;


@Service("attrGroupService")
public class AttrGroupServiceImpl extends ServiceImpl<AttrGroupDao, AttrGroupEntity> implements AttrGroupService {

    @Resource
    @Qualifier("categoryService")
    private CategoryService categoryService;
    @Resource
    @Qualifier("attrService")
    private AttrService attrService;


    @Override
    public PageUtils queryPage(Map<String, Object> params) {
        IPage<AttrGroupEntity> page = this.page(
                new Query<AttrGroupEntity>().getPage(params),
                new QueryWrapper<AttrGroupEntity>()
        );

        return new PageUtils(page);
    }

    /**
     * @param params
     * @param cateLogId
     * @==============History===============<br/>
     * @Description // TODO  根据获取当前三级分类下的属性分组信息
     * @Date 2023/3/15 22:00
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return com.bxait.com.utils.PageUtils
     */
    @Override
    public PageUtils queryPageByCateLogId(Map<String, Object> params, Long cateLogId) {
        // 1.先校验这个三类分类是否是有效的,如果无效直接返回异常,由SpringMvc的统一异常处理类直接处理
        Boolean flag = CateGoryCheck.checkCateLogId(cateLogId);
        if(!flag){
           throw  new CheckOutException(BizCodeEnum.VALID_CHECKOUT_EXCEPTION.getMsg());
        }
        // 2.构造请求参数
        QueryWrapper<AttrGroupEntity>
                wrapper = new QueryWrapper<>();
        String key = (String) params.get("key");
        if(!StringUtils.isEmpty(key)){
            wrapper.and((obj)->{
                obj.eq("attr_group_id",key).or().like("attr_group_name",key);
            });
        }
        // 2.如果前端发来的三类分类id为0,则是需要查询所以分类下的属性分组信息
         if(cateLogId == 0){
             IPage<AttrGroupEntity> page = this.page(
                     new Query<AttrGroupEntity>().getPage(params),
                     wrapper
             );
             return new PageUtils(page);
         }else {
             // 3.如果携带了三级分类的id,就是要查当前三级分类下的属性分组信息
             wrapper.eq("catelog_id",cateLogId);
             IPage<AttrGroupEntity> page = this.page(
                     new Query<AttrGroupEntity>().getPage(params),
                     wrapper
             );
             return new PageUtils(page);
         }
    }

    /**
     * @param attrGroupId  属性的分组
     * @==============History===============<br/>
     * @Description // TODO  获取当前属性分组的信息,并且封装当前属性分组所属分类的全路径
     * @Date 2023/3/15 23:12
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return com.bxait.toymail.product.entity.AttrGroupEntity
     */
    @Override
    public AttrGroupEntity getAttrGroupAndCateLogPath(Long attrGroupId) {
        // 1.获取属性分组的实体信息
        AttrGroupEntity entity = this.getById(attrGroupId);
        // 2.获取当前属性分组所处的三级分类的id
        Long cateLogId = entity.getCatelogId();
        List<Long> paths = new ArrayList<>();
        // 3.返回当前三级分类的全路径
        List<Long> parentPath = findParentPath(cateLogId, paths);
        // 4.设置当前属性分组所属分类的全路径
        entity.setCateLogPath(new Long[parentPath.size()]);
        return entity;
    }

    /**
     * @param cateLogId
     * @==============History===============<br/>
     * @Description // TODO  获取当前分类下的所有分组,及其分组下的所有属性
     * @Date 2023/3/20 20:58
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return java.util.List<com.bxait.toymail.product.utils.AttrGroupWithAttrsVo>
     */
    @Override
    public List<AttrGroupWithAttrsVo> getAttrGroupWithAttrsByCateLogId(Long cateLogId) {
        // 获取当前分类下的所有属性分组
        List<AttrGroupEntity> attrGroupEntities
                = this.baseMapper.selectList(new QueryWrapper<AttrGroupEntity>().eq("catelog_id", cateLogId));

        List<AttrGroupWithAttrsVo> collect = attrGroupEntities.stream().map(item -> {
            AttrGroupWithAttrsVo attrGroupWithAttrsVo = new AttrGroupWithAttrsVo();
            BeanUtils.copyProperties(item, attrGroupWithAttrsVo);
            // 获取当前属性分组下的所有属性
            List<AttrEntity> relationAttr = attrService.getRelationAttr(item.getAttrGroupId());
            attrGroupWithAttrsVo.setAttrs(relationAttr);
            return attrGroupWithAttrsVo;

        }).collect(Collectors.toList());
        return collect;
    }

    /*
     * @param spuId
     * @param catalogId
     * @==============History===============<br/>
     * @Description //   获取当前属性的分组信息,以及分组下的所有属性对应的值
     * @Date 2023/4/17 2023/4/17
     * @Author < a href="2277411582@qq.com">chen</ a>
     * @return [java.lang.Long, java.lang.Long]
     */
    @Override
    public List<SpuItemAttrGroupVo> getAttrGroupWithAttrsBySpuId(Long spuId, Long catalogId) {
        AttrGroupDao baseMapper = this.baseMapper;
        List<SpuItemAttrGroupVo> groupVos = baseMapper.getAttrGroupWithAttrsBySpuId(spuId,catalogId);
        return groupVos;
    }


    /**
     * @param cateLogId 三级分类的id
     * @param paths     当前三级分类的全路径
     * @==============History===============<br/>
     * @Description // TODO  返回当前三级分类的全路径
     * @Date 2023/3/15 23:29
     * @Author <a href="qq:2277411582@qq.com">chen</a>
     * @return java.util.List<java.lang.Long>
     */
    private List<Long> findParentPath(Long cateLogId, List<Long> paths){
        // 1.进入的时候先收集当前分类的id
        paths.add(cateLogId);
        // 2.获取当前分类的实体
        CategoryEntity entity = categoryService.getById(cateLogId);
        // 3.如果当前分类还有父分类,递归查询
        if(entity.getParentCid() != 0){
            findParentPath(entity.getCatId(),paths);
        }
        // 4.递归调用结束,反转这个分类的全路径返回
        Collections.reverse(paths);
        return paths;
    }

}